﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.Admin;

namespace WindowsForms.Admin
{
    public partial class UcBoPhan : UserControl
    {
        public static bool State = false;
        public UcBoPhan()
        {
            InitializeComponent();
            dataGridViewhienthibop.AutoGenerateColumns = false;
        }
        private void btrefresh_Click(object sender, EventArgs e)
        {
            LoadBoPhan(BoPhan.Objects.All().ToList());

        }

        private void LoadBoPhan(List<BoPhan>ds)
        {
            dataGridViewhienthibop.DataSource = ds;
        }
 

        private void btthembophan_Click(object sender, EventArgs e)
        {
            FormThemBoPhan fraddbp = new FormThemBoPhan();
            fraddbp.state = false;
            fraddbp.FormClosed += new FormClosedEventHandler(fraddbp_FormClosed);
            fraddbp.ShowDialog();
        }

        void fraddbp_FormClosed(object sender, FormClosedEventArgs e)
        {
            btrefresh.PerformClick();
        }

        private void dataGridViewhienthibop_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {


                string ma = dataGridViewhienthibop.Rows[e.RowIndex].Cells[0].Value.ToString();
                BoPhan bphan = BoPhan.Objects.GetById(ma);
                if (e.ColumnIndex == 3)
                {


                    if (bphan != null)
                    {

                        BoPhan.Objects.Delete(bphan);
                        btrefresh.PerformClick();
                    }
                }
                if (e.ColumnIndex == 4)
                {
                    FormThemBoPhan frbp = new FormThemBoPhan();
                    FormThemBoPhan.bophan = bphan;
                    frbp.state = true;
                    frbp.FormClosed += new FormClosedEventHandler(frbp_FormClosed);
                    frbp.ShowDialog();
                }
            }
            catch
            {


            }

        }

        void frbp_FormClosed(object sender, FormClosedEventArgs e)
        {
            btrefresh.PerformClick();
        }

        private void bttimbophan_Click(object sender, EventArgs e)
        {
            string data=tbtim.TextBox.Text;
            if(!String.IsNullOrWhiteSpace(data))
            {
                List<BoPhan> ds = BoPhan.Objects.Find(p => p.TenBoPhan.Contains(data)).ToList();
                if(ds.Count>0)
                {
                    LoadBoPhan(ds);
                } 
                else
                    MessageBox.Show("Không Có Bộ Phận Có Tên Như Bạn Tìm");
            }
        }

    }
}
